package com.frinder.robot.handler;

import com.frinder.base.akka.AbstractRequest;
import com.frinder.base.akka.BaseActor;
import com.frinder.robot.strategy.task.distribute.DistributeTaskToRobotHandler;
import com.frinder.robot.strategy.task.distribute.DistributeTaskToRobotPayload;
import lombok.Builder;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import static com.frinder.robot.strategy.task.distribute.DistributeTaskToRobotPayload.Type.PRIORITY_DISTANCE;

/**
 * @ClassName RobotTaskManagerActor
 * @Description 机器人任务管理器
 * @Author frinder
 * @Date 2019/10/19 10:21
 * @Verion 1.0
 */
@Component
@Scope("prototype")
public class RobotTaskManagerActor extends BaseActor {

    public static final String NAME = "robot-task-manager-actor";

    @Autowired
    private DistributeTaskToRobotHandler distributeTaskToRobotHandler;

    @Override
    public void preStart() throws Exception {
        super.preStart();
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(ApplyForRobotTaskPayload.class, this::distribute)
                .build();
    }

    /**
     * 分发任务
     *
     * @param payload
     */
    private void distribute(ApplyForRobotTaskPayload payload) {
        blog.info("{}", payload);
        distributeTaskToRobotHandler.handle(DistributeTaskToRobotPayload.builder()
                .sender(getSelf())
                .receiver(getSender())
                .type(PRIORITY_DISTANCE)
                .build()
        );
    }

    @ToString(callSuper = true)
    public static class ApplyForRobotTaskPayload extends AbstractRequest {
        private String robotName;

        @Builder
        private ApplyForRobotTaskPayload(String traceId, String robotName) {
            super(traceId);
            this.robotName = robotName;
        }
    }
}
